#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>

#include "log.hpp"

using namespace log_namespace;

const static std::string sep = ": ";

class Dict
{   
private:
    void LoadDict(const std::string &dictpath)
    {
        std::ifstream in(dictpath);
        if (!in.is_open())
        {
            LOG(FATAL, "open %s failed!\n", dictpath.c_str());
            exit(1);
        }

        std::string line;
        while (std::getline(in, line))
        {
            LOG(DEBUG, "load info: %s, success!\n", line.c_str());
            if (line.empty())
            {
                continue;
            }
            auto pos = line.find(sep);
            if (pos == std::string::npos)
            {
                continue;
            }
            std::string key = line.substr(0, pos);
            if(key.empty()) continue;
            std::string val = line.substr(pos + sep.size());
            if(val.empty()) continue;
            _dict.insert(std::make_pair(key, val));
        }
        LOG(INFO, "load %s done\n", dictpath.c_str());

        in.close();
    }
public:
    Dict(const std::string &dictpath)
        :_dictPath(dictpath)
    {
        LoadDict(_dictPath);
    }

    std::string translate(std::string word)
    {
        if (word.empty())
        {
            return "None";
        }
        auto it = _dict.find(word);
        if (it == _dict.end())
        {
            return "None";
        }
        else
        {
            return it->second;
        }
    }

    ~Dict()
    {
        
    }
private:
    std::unordered_map<std::string, std::string> _dict;
    std::string _dictPath;
};
